home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Format 1995 June
/
MacFormat 25.iso
/
Shareware City
/
Developers
/
ICProgKit1.0
/
Source
/
Libs
/
ICMappings.p
< prev
next >
Wrap
Text File
|
1994-12-03
|
5KB
|
181 lines
unit ICMappings;
interface
uses
{$ifc undefined THINK_Pascal}
Types, Files, Aliases, Errors,
{$endc}
ICTypes, ICAPI, ICKeys;
function ICMCountEntries (entries: Handle; var count: longint): ICError;
function ICMGetEntry (entries: handle; pos: longInt; var entry: ICMapEntry): ICError;
function ICMGetIndEntry (entries: handle; ndx: longint; var pos: longint; var entry: ICMapEntry): ICError;
function ICMAddEntry (entries: handle; var entry: ICMapEntry): ICError;
function ICMDeleteEntry (entries: handle; pos: longint): ICError;
function ICMSetEntry (entries: handle; pos: longInt; var entry: ICMapEntry): ICError;
implementation
{$ifc undefined THINK_Pascal}
uses
Memory, ToolUtils;
{$endc}
function UnpackEntry (entries: handle; pos: longInt; var entry: ICMapEntry; var user_length: longInt): OSErr;
(* WARNING: Depends very much on the exact format of ICMapEntry! *)
procedure CopyString (var p: ptr; var s: str255);
var
len: integer;
begin
len := BAND(p^, $FF) + 1;
BlockMove(p, @s, len);
p := ptr(ord(p) + len);
end;
var
org: Ptr;
p: ptr;
maxsize: longInt;
err: OSErr;
begin
err := noErr;
if (entries = nil) | (entries^ = nil) | (pos < 0) | (pos > GetHandleSize(entries) - 6) then begin
err := paramErr;
end;
if err = noErr then begin
p := (ptr(ord(entries^) + pos));
maxsize := GetHandleSize(entries);
org := p;
BlockMove(p, @entry, 6);
if (entry.fixed_length <> ICmap_fixed_length) | (entry.fixed_length > entry.total_length) | (entry.total_length > maxsize) then begin
err := badExtResource;
end;
end;
if err = noErr then begin
BlockMove(p, @entry, entry.fixed_length);
p := ptr(ord(p) + entry.fixed_length);
CopyString(p, entry.extension);
CopyString(p, entry.creator_app_name);
CopyString(p, entry.post_app_name);
CopyString(p, entry.MIME_type);
CopyString(p, entry.entry_name);
user_length := entry.total_length - (ord(p) - ord(org));
end;
UnpackEntry := err;
end;
procedure PackEntry (var entry: ICMapEntry; p: ptr; user_length: longInt);
procedure CopyString (var s: str255);
begin
BlockMove(@s, ptr(ord(p) + entry.total_length), length(s) + 1);
entry.total_length := entry.total_length + length(s) + 1;
end;
begin
entry.version := 0;
entry.fixed_length := ord(@entry.extension) - ord(@entry);
entry.total_length := entry.fixed_length;
CopyString(entry.extension);
CopyString(entry.creator_app_name);
CopyString(entry.post_app_name);
CopyString(entry.MIME_type);
CopyString(entry.entry_name);
entry.total_length := entry.total_length + user_length;
BlockMove(@entry, p, entry.fixed_length);
end;
function ICMDeleteEntry (entries: handle; pos: longint): ICError;
var
entry: ICMapEntry;
junk: longint;
user_length: longInt;
err: OSErr;
begin
err := UnpackEntry(entries, pos, entry, user_length);
if err = noErr then begin
junk := Munger(entries, pos, nil, entry.total_length, Ptr(-1), 0);
err := MemError;
end;
ICMDeleteEntry := err;
end; (* ICMDeleteEntry *)
function GetShort (p: Ptr): integer;
begin
GetShort := BAND(p^, $FF) * 256 + BAND(ptr(ord(p) + 1)^, $FF);
end;
function ICMCountEntries (entries: Handle; var count: longint): ICError;
var
p: Ptr;
pos: longint;
size: integer;
begin
p := entries^;
pos := 0;
count := 0;
while pos < GetHandleSize(entries) do begin
size := GetShort(p);
pos := pos + size;
p := ptr(ord(p) + size);
count := count + 1;
end; (* while *)
ICMCountEntries := noErr;
end; (* ICMCountEntries *)
function ICMGetEntry (entries: handle; pos: longInt; var entry: ICMapEntry): ICError;
var
user_length: longInt;
begin
ICMGetEntry := UnpackEntry(entries, pos, entry, user_length);
end;
function ICMGetIndEntry (entries: handle; ndx: longint; var pos: longint; var entry: ICMapEntry): ICError;
var
err: ICError;
p: Ptr;
i: longint;
size: integer;
begin
p := entries^;
pos := 0;
while (ndx > 1) & (pos < GetHandleSize(entries)) do begin
size := GetShort(p);
pos := pos + size;
p := Ptr(ord(p) + size);
ndx := ndx - 1;
end; (* while *)
ICMGetIndEntry := ICMGetEntry(entries, pos, entry);
end; (* ICMGetIndEntry *)
function ICMAddEntry (entries: handle; var entry: ICMapEntry): ICError;
var
e: ICMapEntry;
begin
PackEntry(entry, @e, 0);
ICMAddEntry := PtrAndHand(@e, entries, entry.total_length);
end;
function ICMSetEntry (entries: handle; pos: longInt; var entry: ICMapEntry): ICError;
var
err: ICError;
e: ICMapEntry;
oldentry: ICMapEntry;
user_length: longInt;
source_length: longInt;
junk: longInt;
begin
err := UnpackEntry(entries, pos, oldentry, user_length);
if err = noErr then begin
PackEntry(entry, @e, user_length);
source_length := oldentry.total_length - user_length;
if user_length < 8 then begin { hack to remove alignment bytes from previous version }
source_length := oldentry.total_length;
e.total_length := e.total_length - user_length;
user_length := 0;
end;
junk := Munger(entries, pos, nil, source_length, @e, e.total_length - user_length);
err := MemError;
end;
ICMSetEntry := err;
end;
end. (* ICMappings *)